package com.its.us.sk.server.tcp.base;

import com.its.us.sk.server.tcp.config.NettyTcpConfig;
import com.its.us.sk.server.ws.config.NettyWsConfig;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author: master
 * @Date: 2020/11/24 8:57
 * @Version: 1.0
 */
public abstract class BaseTcpServerHandler extends BaseTcpHandler {
    // 全局单例实例化唯一
    //public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    /**
     * 一旦链接，第一个被执行
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        String nowTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        // channelGroup.writeAndFlush 会遍历并将消息发送 不需要自己遍历
        NettyTcpConfig.getChannelGroup().writeAndFlush("客户端：【" + ctx.channel().remoteAddress() + "】加入了！时间：" + nowTime);
        NettyTcpConfig.getChannelGroup().add(ctx.channel());
        System.err.println("tcp人数：" + NettyTcpConfig.getChannelGroup().size());
        String msg = "【tcp客户端：" + ctx.channel().remoteAddress() + "】欢迎连接！时间：" + nowTime.getBytes();
        ctx.writeAndFlush(URLEncoder.encode(msg, "UTF-8"));
    }

    /**
     * 断开连接
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        String nowTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        NettyTcpConfig.getChannelGroup().writeAndFlush("tcp客户端：【" + channel.remoteAddress() + "】离开了！时间：" + nowTime);
        // 一旦触发此事件，会自动将 channelGroup 中的channel remove 掉
        // 全局需要手动触发
        NettyTcpConfig.getChannelGroup().remove(ctx.channel());
    }

    /**
     * 处于活动状态
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        String nowTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.err.println("tcp:"+ctx.channel().remoteAddress() + "：上线了！时间：" + nowTime);
    }

    /**
     * 处于非活动状态
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        String nowTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.err.println(ctx.channel().remoteAddress() + "：离线了！时间：" + nowTime);
        System.err.println("tcp channelGroup size:" + NettyTcpConfig.getChannelGroup().size());
    }


    /**
     * 发生异常关闭通道
     *
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.channel().close();
        System.err.println("异常：" + cause);
    }

    /**
     * 真正业务处理
     *
     * @param ctx
     * @param buffer
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
        this.channelRead(ctx, buffer);
    }

    protected abstract void channelRead(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception;
}
